package com.ibeeking.found.file.rest.controller.v1;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ibeeking.found.file.rest.service.IShareFileService;
import com.ibeeking.found.file.rest.service.component.FileDealComp;
import com.ibeeking.found.file.rest.service.impl.UserFileServiceImpl;
import com.ibeeking.found.file.rest.service.util.QiwenFile;
import com.ibeeking.found.file.service.common.dos.ShareFileDO;
import com.ibeeking.found.file.service.common.dos.UserFileDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;
/**
 * @ClassName TaskController
 * @Description 执行任务管理
 * @Author ibeeking
 * @Date 2021-06-02 18:15
 **/
@Slf4j
@Controller
public class TaskController {

    @Resource
    private UserFileServiceImpl userFileServiceImpl;

    @Resource
    private FileDealComp fileDealComp;

    @Resource
    private IShareFileService shareFileServiceImpl;

    @Resource
    private ElasticsearchClient elasticsearchClient;


    @Scheduled(fixedRate = 1000 * 60 * 60 * 24)
    public void updateElasticSearch() {
        List<UserFileDO> userfileList = userFileServiceImpl.list(new QueryWrapper<UserFileDO>().eq("deleteFlag", 0));
        for (int i = 0; i < userfileList.size(); i++) {
            try {

                QiwenFile ufopFile = new QiwenFile(userfileList.get(i).getFilePath(), userfileList.get(i).getFileName(), userfileList.get(i).getIsDir() == 1);
                fileDealComp.restoreParentFilePath(ufopFile, userfileList.get(i).getUserId());
                if (i % 1000 == 0 || i == userfileList.size() - 1) {
                    log.info("目录健康检查进度：" + (i + 1) + "/" + userfileList.size());
                }

            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
        userfileList = userFileServiceImpl.list(new QueryWrapper<UserFileDO>().eq("deleteFlag", 0));
        for (UserFileDO userFile : userfileList) {
            fileDealComp.uploadESByUserFileId(userFile.getUserFileId());
        }

    }

    @Scheduled(fixedRate = Long.MAX_VALUE)
    public void updateFilePath() {
        List<UserFileDO> list = userFileServiceImpl.list();
        for (UserFileDO userFile : list) {
            try {
                String path = QiwenFile.formatPath(userFile.getFilePath());
                if (!userFile.getFilePath().equals(path)) {
                    userFile.setFilePath(path);
                    userFileServiceImpl.updateById(userFile);
                }
            } catch (Exception e) {
                // ignore
            }
        }
    }

    @Scheduled(fixedRate = Long.MAX_VALUE)
    public void updateShareFilePath() {
        List<ShareFileDO> list = shareFileServiceImpl.list();
        for (ShareFileDO shareFile : list) {
            try {
                String path = QiwenFile.formatPath(shareFile.getShareFilePath());
                shareFile.setShareFilePath(path);
                shareFileServiceImpl.updateById(shareFile);
            } catch (Exception e) {
                //ignore
            }
        }
    }
}
